/*!
 * Ext JS Library 4.0
 * Copyright(c) 2006-2011 Sencha Inc.
 * licensing@sencha.com
 * http://www.sencha.com/license
 */

Ext.define('MyDesktop.App', {
    extend: 'Ext.ux.desktop.App',

    requires: [
        'Ext.window.MessageBox',
        'Ext.ux.desktop.ShortcutModel',
        'MyDesktop.BogusMenuModule',
        'MyDesktop.BogusModule',
        'MyDesktop.Settings'
    ],

    init: function() {
        // custom logic before getXYZ methods get called...
    	this.StartMenuList = this.getStartMenu();
        this.callParent();

        // now ready...
    },
    /**
     * 获取开始菜单
     */
    getStartMenu: function(){
    	var StartMenuList = [];
    	var me = this;
		AjaxUtils.CallJsonService({
			url: CONTEXT.contextPath+"/v1/navmenu",
			dataType: "json",
			call: function(data){
				if(data.status == "OK"){
					//开始菜单
					var list = data.entity.menu;
					for(i in list){
						var menu = list[i];
						var text = menu.menu_name;
						var id = menu.rwid;
						var parentid = menu.parent_menu_id;
						var has_child = menu.is_child_menu;
						if(parentid =="00000000000000000000000000000001"){
							//定义配置
							var config = {
									launcher:{
										text: text,
										iconCls: 'icon-grid',
										handler: function() {
					    	                return false;
					    	            }
									}
							}
							//存在子菜单
							if(has_child == Constants.YES){
								config.launcher.menu={
										minWidth: 127,
										items: []
								};
								me.getStartChildMenu(list,id,config.launcher.menu.items);
							}
							
							StartMenuList.push(config);
						}
					}
					//桌面快捷菜单
					var deskmenu = me.getDesktopConfig();
					var desklist = data.entity.desktop;
					var records = [];
					for(i in desklist){
						var menu = desklist[i];
						var text = menu.menu_name;
						var id = menu.rwid;
						var window_id = "menu_"+id;
						var iconImg_48 = menu.icon_url;
						var iconImg_32 = "";
						//设置默认桌面图标
						if(Ext.isEmpty(iconImg_48)){
							iconImg_48 = "icon_default_48";
							iconImg_32 = "icon_default_32";
						}else{
							iconImg_32 = iconImg_48.substring(0,iconImg_48.lastIndexOf("_"))+"_32";
						}
						records.push({
							"name":text,
							"iconCls":iconImg_48,
							"iconCls_32": iconImg_32,
							"module":"",
							"menu_id":id,
							"menu_text":text,
							"window_id":window_id
						});
					}
					me.shortcuts = records;
					var pageBtnMap = data.entity.userMenuBtn;
					Utils.setAllPageBtn(pageBtnMap);
					var modelBtnMap = data.entity.menuModelBtnMap;
					Utils.setAllModelBtn(modelBtnMap);
				}else{
					AjaxUtils.showError('加载系统菜单异常');
				}
			}
		});
		return StartMenuList;
    },
    /**
     * 获取子菜单
     * @param menulist 菜单列表
     * @param father_menu_id 父菜单id
     * @param menu_config 菜单配置
     */
    getStartChildMenu: function(menulist,father_menu_id,menu_config){
    	var me = this;
    	for(i in menulist){
			var menu = menulist[i];
			var text = menu.menu_name;
			var id = menu.rwid;
			var parentid = menu.parent_menu_id;
			var has_child = menu.is_child_menu;
			var iconImg_48 = menu.icon_url;
			var iconImg_32 = "";
			//设置默认桌面图标
			if(Ext.isEmpty(iconImg_48)){
				iconImg_48 = "icon_default_48";
				iconImg_32 = "icon_default_32";
			}else{
				iconImg_32 = iconImg_48.substring(0,iconImg_48.lastIndexOf("_"))+"_32";
			}
			if(parentid == father_menu_id){
				var win_id = 'win_'+id;
				menu_config.push({
					text: text,
					id: win_id+"_m",
					menuid: id,
		            maximized: false,
		            iconCls: 'icon-grid',
		            iconCls_32: iconImg_32,
		            scope: this,
		            has_child: has_child,
		            handler: function(src) {
		            	if(src.has_child == Constants.YES){
		            		return null;
		            	}
		                var desktop = this.desktop;
		                //窗体
		                var window_id = "menu_"+src.menuid;
		                var win = desktop.getWindow(window_id);
		                if(!win){
		                	//菜单编码
		                	var menu_id = src.menuid;
		                	//菜单名称
		                	var menu_text = src.text;
		                	var menu_iconCls_32 = src.iconCls_32;
		                    win = desktop.createWindow({
		                        id: window_id,
		                        title: menu_text,
		                        width: Utils.getWidth()*0.8,
		                        height: Utils.getHeight()*0.8,
		                        iconCls: menu_iconCls_32,
		                        //icon: CONTEXT.contextPath + '/web/theme/desktop/images/desktop-icon/icon_default_32.png',
		                        animCollapse:false,
		                        border: false,
		                        hideMode: 'offsets',
		                        layout: 'fit',
		                        tools:[{
		                        	type: 'plus',
		                        	tooltip: '创建桌面快捷方式',
		                        	window_id: window_id,
		                        	menu_id: menu_id,
		                            handler: function(event, toolEl, panel){
		                            	var w = this;
		                            	AjaxUtils.showConfirm(function(){
		                            		desktop.addShortCutMenu(w);
		                            	},"确定将当前功能添加到桌面？");
		                            }
		                        },{
		                            type:'refresh',
		                            tooltip: '刷新窗体',
		                            handler: function(event, toolEl, panel){
		                            	var frame = document.getElementById("frame_"+menu_id);
		                            	frame.src = frame.src;
		                            }
		                        },
		                        {
		                            type:'help',
		                            tooltip: '功能帮助',
		                            handler: function(event, toolEl, panel){
		                            	AjaxUtils.showInfo("当前功能暂未开放")
		                            }
		                        }],
		                        html: '<iframe id="frame_'+menu_id+'" name="frame_'+menu_id+'" src="'+CONTEXT.contextPath+'/nav?rwid='+menu_id+'" style="width:100%;height:100%;" frameborder="0" scrolling="no"></iframe>'
		                    });
		                }
		                win.show();
		                return win;
		            }
				});
				if(has_child == Constants.YES){
					//debugger;
					menu_config[menu_config.length-1].menu={
							minWidth: 127,
			            	items:[]
					};
					me.getStartChildMenu(menulist,id,menu_config[menu_config.length-1].menu.items);
				}
				
			}
    	}
    },
    //开始菜单
    getModules : function(){
    	var menu = this.StartMenuList;
    	return menu;
    },

    getDesktopConfig: function (menudata) {
        var me = this, ret = me.callParent();
        
        return Ext.apply(ret, {
            //cls: 'ux-desktop-black',
        	//右击快捷菜单
            contextMenuItems: [
                { text: '设置', handler: me.onSettings, scope: me ,iconCls:'settings'},
                '-',
                { text: '快捷菜单管理', handler: me.shortCutMenu, scope: me,iconCls:'shortcut' },
                '-',
                { text: '刷新', handler: function(){
                	window.location.reload();
                }, scope: me,iconCls:'refresh' }
            ],
        	//桌面快捷菜单
            shortcuts: Ext.create('Ext.data.Store', {
                model: 'Ext.ux.desktop.ShortcutModel',
                data:me.shortcuts
            }),

            wallpaper: CONTEXT.contextPath+'/web/theme/desktop/wallpapers/Blue-Sencha.jpg',
            wallpaperStretch: false
        });
    },
    /**
     * 获取系统开始菜单
     */
    getStartConfig : function() {
        var me = this, ret = me.callParent();

        return Ext.apply(ret, {
            title: '系统程序',
            iconCls: 'user',
            height: 300,
            toolConfig: {
                width: 100,
                items: [
                    {
                        text:'设置',
                        iconCls:'settings',
                        handler: me.onSettings,
                        scope: me
                    },
                    '-',
                    {
                        text:'退出',
                        iconCls:'logout',
                        handler: me.onLogout,
                        scope: me
                    }
                ]
            }
        });
    },
    //快速启动栏
    getTaskbarConfig: function () {
        /*var ret = this.callParent();
        return Ext.apply(ret, {
            quickStart: [
                { name: 'Accordion Window', iconCls: 'accordion', module: 'acc-win' },
                { name: 'Grid Window', iconCls: 'icon-grid', module: 'grid-win' }
            ],
            trayItems: [
                { xtype: 'trayclock', flex: 1 }
            ]
        });*/
    	return this.callParent();;
    },
    //退出系统
    onLogout: function () {
    	AjaxUtils.showConfirm(function(){
    		window.location.href = CONTEXT.contextPath + "/logout";
    	},"确定需要退出系统？");
    },
    //系统设置
    onSettings: function () {
        var dlg = new MyDesktop.Settings({
            desktop: this.desktop
        });
        dlg.show();
    },
    /**
     * 桌面快捷菜单管理
     */
    shortCutMenu: function(){
		var win = Ext.create('Ext.com.bkfm.window', {
			title : "桌面快捷菜单管理",
			autoScroll : true,
			modal : true,
			height : Utils.getHeight()*0.8,
			width : Utils.getWidth()*0.7,
			items: [Ext.create('Ext.com.bkfm.datagrid', {
						id: "shortcutsgrid",
						objPath : "ROOT/XTGLPT/XTSZ/BLK_USER_SHORTCUTS",
						border: true,
						filter: " USER_NO='"+Utils.getCurrentUser().user.user_no+"'",
						customBtn: [{
							text: '删除',
							iconCls: 'delete',
							handler: function(){
								Ext.getCmp("shortcutsgrid").deleteRecords();
							}
						}]
					})
			]
		});
		win.show();
    }
});
